<?php
session_start();
require_once("config.php");
if ( $_SERVER['REQUEST_METHOD'] == "GET" ) {
//    if(array_key_exists('data', $_GET)){
        $data = json_decode($_SESSION['basket'],true);

        foreach($data as $item){
            $ids.='\''.$item['Article'].'\',';
        }
        $ids=substr($ids, 0, -1);


        $select="SELECT a.Price price, a.Article article, "."b.FileName name, a.`Origin country` country FROM melagranoru.titems_internet a
                    inner join melagranoru.tmatchingphotos b
                    on a.Article = b.linkArticle
                    where a.Article in (".$ids.") and b.`Order`=1
                    group by id";


        $r = mysql_query($select,$link);

        for($i = mysql_num_rows($r); $i > 0 ; $i--) {
            $row = mysql_fetch_assoc($r);
            $row["id"]= $row["article"];
            $content[] = $row;
        }
//    }


    echo json_encode($content);
}
/** UPDATE or DELETE **/
else if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
//    $_SESSION['basket']="";
//    unset($_SESSION['basket']);
//    $_SESSION['basket']= array();
    if(isset($_POST['_method']) && $_POST['_method']=="DELETE"){
        $var = json_decode($_SESSION[basket],true);
        for($i=0;$i<sizeof($var);++$i){
            if($var[$i]!=null && $var[$i]["Article"]==substr($_SERVER["PATH_INFO"],1)){
                unset($var[$i]);
                $_SESSION['basket']=json_encode($var);
                break;
            }
        }
    }else{


    if(!array_key_exists('basket', $_SESSION)){
        $_SESSION['basket']= array();
    }

    $var = json_decode($_SESSION[basket],true);
    $array[]=json_decode($_POST['model'],true);

    if(is_array($var) && is_array($array)){
        $array=array_merge($array,$var);
    }
    $_SESSION['basket']=json_encode($array);
    }
    //json_decode($_POST['model'],true)
}

else {
    // Just return a model. Don't bother actually creating it and returning the new record
    $data = array( "id" => 1, "name" => "Joe Zim", "age" => 23 );
    // Encode it as JSON and ship it back
    echo json_encode($data);
}


?>
